package com.kly.cashmall.module.profile;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;

import com.google.firebase.analytics.FirebaseAnalytics;
import com.kly.cashmall.AppApplication;
import com.kly.cashmall.R;
import com.kly.cashmall.base.BaseBarActivity;
import com.kly.cashmall.base.app_action.ActionHeads;
import com.kly.cashmall.base.app_action.ActionParams;
import com.kly.cashmall.base.app_action.BaseActionHelper;
import com.kly.cashmall.bean.ProfileInfoBean;
import com.kly.cashmall.bean.ProfileJumpBean;
import com.kly.cashmall.event.FirebaseAnalyticsEvent;
import com.kly.cashmall.event.HomeEvent;
import com.kly.cashmall.framework.mvp.PresenterLifeCycle;
import com.kly.cashmall.module.profile.adapter.GeneralInfoAdapter;
import com.kly.cashmall.module.profile.presenter.GeneralInfoPresenter;
import com.kly.cashmall.module.profile.presenter.GeneralInfoViewer;
import com.kly.cashmall.utils.function.ToastUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * @author
 */
public class GeneralInfoActivity extends BaseBarActivity implements GeneralInfoViewer {

    @PresenterLifeCycle
    GeneralInfoPresenter presenter = new GeneralInfoPresenter(this);
    private ListView mPersonalInfoList;
    private GeneralInfoAdapter mPersonInfoAdapter;
    private TextView mSubmitBtn;
    private static final String TYPE = "type";
    private static final String TITLE = "title";
    private long time;
    private NestedScrollView mNestScrollView;

    public static Intent callIntent(Context context, String type, String title) {
        Intent intent = new Intent(context, GeneralInfoActivity.class);
        intent.putExtra(TYPE, type);
        intent.putExtra(TITLE, title);
        return intent;
    }

    @Override
    protected void setView(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_general_info);
        setTitle(getIntent().getStringExtra(TITLE));
        time = System.currentTimeMillis();
        mNestScrollView = bindView(R.id.nest_scroll_view);
        mPersonalInfoList = findViewById(R.id.personal_info_list);
        mPersonInfoAdapter = new GeneralInfoAdapter(this);
        mPersonalInfoList.setAdapter(mPersonInfoAdapter);
        mSubmitBtn = findViewById(R.id.submit_btn);
        mSubmitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPersonInfoAdapter != null) {
                    mPersonInfoAdapter.clearFocus();
                    if (mPersonInfoAdapter.getParams() != null) {
                        presenter.savePersonInfo(mPersonInfoAdapter.getParams(),getIntent().getStringExtra(TYPE));
                    } else {
                        ToastUtil.showAppToast(getString(R.string.profile_children_submit));
                    }
                }
            }
        });
        mPersonalInfoList.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState) {

                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:    //当停止滚动时

                        break;
                    case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:    //滚动时
                        break;
                    case AbsListView.OnScrollListener.SCROLL_STATE_FLING:   //手指抬起，但是屏幕还在滚动状态

                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }

    @Override
    protected void loadData() {
        presenter.getPersonInfo(getIntent().getStringExtra(TYPE));
    }

    @Override
    public void getPersoninfoSuccess(ProfileInfoBean infoBean) {
        mSubmitBtn.setVisibility(infoBean.buttonVisibility ? View.VISIBLE : View.GONE);
        mPersonInfoAdapter.setPersonInformationBean(infoBean);
    }

    @Override
    public void savePersonInfoSuccess(ProfileJumpBean jumpBean) {
        FirebaseAnalytics.getInstance(AppApplication.getInstance()).logEvent(FirebaseAnalyticsEvent.Profile_Save+"_"+getIntent().getStringExtra(TITLE),new Bundle());

        EventBus.getDefault().post(new HomeEvent());
        if (!jumpBean.getJumpUrl().contains(ActionHeads.LMActionProfile)) {
            BaseActionHelper.with(this).handleAction(jumpBean.getJumpUrl());
            finish();
        } else {
            String type = ActionParams.parse(jumpBean.getJumpUrl()).get("type", "");
            mNestScrollView.scrollTo(0, 0);
            presenter.getPersonInfo(type);

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (mPersonInfoAdapter != null) {
            mPersonInfoAdapter.onActivityResult(requestCode, resultCode, data);
        }
    }
}
